package com.ch.forum.dao;

import com.ch.forum.bo.ArtVo;
import com.ch.forum.model.ArtModel;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticelMapper {

    @Insert("insert into t_article(click_num,content,date,is_pre,is_public,price_num,titile,unit_price,unit_watch_time,user_id,zan_num,img_url)" +
            " values(#{model.clickNum},#{model.content},#{model.date},#{model.isPre},#{model.isPublic},#{model.priceNum},#{model.titile},#{model.unitPrice},#{model.unitWatchTime},#{model.userId},#{model.zanNum},#{model.imgUrl})")
    @Options(useGeneratedKeys = true, keyProperty = "model.id")
    void addArticel(@Param("model") ArtModel model);

    @Select("select t.id,t.click_num as clickNum,t.content,t.date,t.is_public as isPublic,t.price_num as priceNum,t.titile," +
            "t.unit_price as unitPrice,t.unit_watch_time as unitWatchTime,t.user_id as userId,t.zan_num as zanNum,u.nick_name as name,(SELECT count(1) FROM  t_reply r WHERE r.art_id = t.id ) as replyNum " +
            "from t_article t,t_user u where t.is_pre = 0 AND u.id = t.user_id and t.id = ${id}")
    ArtVo findArtById(@Param("id")Long id);

    @Select("select id from t_article where is_pre = 1 order by date desc")
    List<Long> findPrdArts();

    @Select("select t.id,t.click_num as clickNum,t.content,t.date,t.is_public as isPublic,t.price_num as priceNum,t.titile,t.unit_price as unitPrice,t.unit_watch_time as unitWatchTime,t.user_id as userId,t.zan_num as zanNum,u.nick_name as name,(SELECT count(1) FROM  t_reply r WHERE r.art_id = t.id ) as replyNum,t.img_url as artImg,u.img_url as userImg " +
            "from t_article t,t_user u where t.is_pre = 0 AND u.id = t.user_id order by t.click_num,t.zan_num desc limit #{no},#{size}")
    List<ArtVo> findArtListPage(@Param("no") int no, @Param("size") int size);

    @Select("select id,click_num as clickNum,content,date,is_public as isPublic,price_num as priceNum,titile,unit_price as unitPrice,unit_watch_time as unitWatchTime,user_id as userId,zan_num as zanNum " +
            "from t_article where is_pre = 0 order by date desc limit #{no},#{size}")
    List<ArtModel> findArtLatestPage(@Param("no") int no, @Param("size") int size);

    @Select("select t.id,t.click_num as clickNum,t.content,t.date,t.is_public as isPublic,t.price_num as priceNum,t.titile,t.unit_price as unitPrice,t.unit_watch_time as unitWatchTime,t.user_id as userId,t.zan_num as zanNum,u.nick_name as name,(SELECT count(1) FROM  t_reply r WHERE r.art_id = t.id ) as replyNum,t.img_url as artImg,u.img_url as userImg " +
            "from t_article t,t_user u where t.is_pre = 0 AND t.user_id = #{userId} AND u.id = t.user_id order by t.click_num,t.zan_num desc limit #{no},#{size}")
    List<ArtVo> findArtListByUserId(@Param("userId")Long userId, @Param("no") int no, @Param("size") int size);

    @Select("select t.id,t.click_num as clickNum,t.content,t.date,t.is_public as isPublic,t.price_num as priceNum,t.titile,t.unit_price as unitPrice,t.unit_watch_time as unitWatchTime,t.user_id as userId,t.zan_num as zanNum,u.nick_name as name,(SELECT count(1) FROM  t_reply r WHERE r.art_id = t.id ) as replyNum,t.img_url as artImg,u.img_url as userImg " +
            "from t_article t,t_user u where t.is_pre = 0 AND u.id = t.user_id order by t.click_num,t.zan_num desc")
    List<ArtVo> findArtList();

    @Select("select count(0) from t_article where is_pre = 0")
    int findArtTotal();

    @Select("select count(0) from t_article t where t.is_pre = 0 AND t.user_id = #{userId} ")
    int countArtListByUserId(@Param("userId")Long userId);

    @Select("select id,click_num as clickNum,content,date,is_pre as isPre,is_public as isPublic,price_num as priceNum,titile,unit_price as unitPrice,unit_watch_time as unitWatchTime,user_id as userId,zan_num as zanNum " +
            "from t_article where is_pre = 0 and (titile like #{key} or content like #{key})  order by date desc limit #{no},#{size}")
    List<ArtModel> findSearchKeyWordsPage(@Param("no") int no, @Param("size") int size, @Param("key") String key);

    @Select("select count(0) from t_article where is_pre = 0 and (titile like #{key} or content like #{key}) ")
    int findSearchTotal(@Param("key") String key);

    @Update("update t_article set click_num = #{num} where id = #{id}")
    int updateClickNum(@Param("id") Long id, @Param("num") int num);

    @Update("update t_article set zan_num = #{num} where id = #{id}")
    int updateZanNum(@Param("id") Long id, @Param("num") int num);

    @Update("update t_article set is_pre = 0 where id in (#{ids})")
    int updateIsPre(@Param("ids")String ids);

    @Select("select t.user_id from t_article t where t.id = #{id}")
    Long findUserIdByArtId(@Param("id")Long id);
}
